import 'dart:math' as math;

import 'package:flutter/widgets.dart';
import 'package:mobile_app.core/core/presentation/theme/theme.dart';

class ActivityIndicator extends StatefulWidget {
  const ActivityIndicator({
    super.key,
  });

  @override
  State<ActivityIndicator> createState() => _ActivityIndicatorState();
}

class _ActivityIndicatorState extends State<ActivityIndicator> with SingleTickerProviderStateMixin {
  late AnimationController _controller;

  @override
  void initState() {
    super.initState();

    _controller = AnimationController(
      duration: const Duration(seconds: 1),
      vsync: this,
    );

    _controller.repeat();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    const indicatorRadius = 28.0;

    return Stack(
      fit: StackFit.expand,
      children: [
        SizedBox(
          width: double.infinity,
          height: double.infinity,
          child: Center(
            child: ClipRRect(
              borderRadius: BorderRadius.circular(10),
              child: Container(
                color: CoreTheme.of(context).activityIndicatorBackgroundColor,
                width: 150,
                height: 150,
                child: SizedBox(
                  height: indicatorRadius * 2,
                  width: indicatorRadius * 2,
                  child: CustomPaint(
                    painter: _ActivityIndicatorPainter(
                      position: _controller,
                      activeColor: CoreTheme.of(context).activityIndicatorColor,
                      radius: indicatorRadius,
                    ),
                  ),
                ),
              ),
            ),
          ),
        ),
        const ModalBarrier(
          dismissible: false,
        ),
      ],
    );
  }
}

const double _kDefaultIndicatorRadius = 9.5;
const double _kTwoPI = math.pi * 2.0;
const int _kTickCount = 12;

// The list has a length of 12.
//const List<int> _alphaValues = <int>[147, 131, 114, 97, 81, 64, 47, 47, 47, 47, 47, 47];
//const List<int> _alphaValues = <int>[255, 239, 222, 205, 189, 172, 155, 155, 155, 155, 155, 155];
const List<int> _alphaValues = <int>[255, 237, 219, 201, 183, 165, 147, 147, 147, 147, 147, 147];

class _ActivityIndicatorPainter extends CustomPainter {
  _ActivityIndicatorPainter({
    required this.position,
    required this.activeColor,
    required double radius,
  })  : tickFundamentalRRect = RRect.fromLTRBXY(
          -radius * 1.08,
          radius / _kDefaultIndicatorRadius,
          -radius / 1.95,
          -radius / _kDefaultIndicatorRadius,
          radius / _kDefaultIndicatorRadius,
          radius / _kDefaultIndicatorRadius,
        ),
        super(repaint: position);

  final Animation<double> position;
  final RRect tickFundamentalRRect;
  final Color activeColor;

  @override
  void paint(Canvas canvas, Size size) {
    final paint = Paint();

    canvas.save();
    canvas.translate(size.width / 2.0, size.height / 2.0);

    final activeTick = (_kTickCount * position.value).floor();

    for (var i = 0; i < _kTickCount; ++i) {
      final t = (i + activeTick) % _kTickCount;
      paint.color = activeColor.withAlpha(_alphaValues[t]);
      canvas.drawRRect(tickFundamentalRRect, paint);
      canvas.rotate(-_kTwoPI / _kTickCount);
    }

    canvas.restore();
  }

  @override
  bool shouldRepaint(_ActivityIndicatorPainter oldPainter) {
    return oldPainter.position != position || oldPainter.activeColor != activeColor;
  }
}
